#!/usr/bin/python
# -*- coding: utf-8 -*-
import omen.engine.core.services as serv
import omen.tools.configuration as config
import time

class CronServiceParamKeys(object):
    TIME_SLICE = 'frequence_in_minutes'
    

class CronServiceImpl(serv.CronService):
    def __init__(self, service_name, conf):
        super(CronServiceImpl, self).__init__(service_name)
        self._last_execution = 0
        self._conf = None
        try:
            config.ConfigurationChecker.check_keys(conf, [CronServiceParamKeys.TIME_SLICE])
            self._conf = conf
        except config.ConfigurationCheckerError as err:
            raise serv.ServiceInitError, err
        
        self._frequence = self._conf[CronServiceParamKeys.TIME_SLICE]
        
    def execute(self):
        now = time.time()
        diff = now - self._last_execution
        if diff > self._frequence:
            self._last_execution = now
            self._treatment()
            
    def _treatment(self):
        pass
